import Vue from 'vue';
import Request from "@/sdk/pocky-request/index.js";
var vm=new Vue();
/**
 * car store 购物车
 */
const car = {
    // 状态
    state: {
		carList:[],
    },
    // 内部派生
    getters: {

    },
    // 同步方法 commit
    mutations: {
		carList: (state, payload) => {
			state.carList = payload
			let r=[];
			payload.items.forEach((x)=>{
				x.isSelect=false;
				x.running=1;
				r.push(x);
			})
			state.carList.items = r;
			
		},
		updateNum: (state, payload) => {
			state.carList.items.forEach((item)=>{
				if(item.id==payload.id){
					item.num=parseInt(payload.num) 
				}
			})
		},
		delGood:(state,payload)=>{
			state.carList.items=state.carList.items.filter((x)=>{
				return x.id!=payload.id
			})
		},
		selectAll:(state,payload)=>{
			let r=[];
			state.carList.items.forEach((x)=>{
				x.isSelect=payload;
				r.push(x);
			})
			state.carList.items = r;
		}
    },
    // 异步方法 dispatch
    actions: {
		/**
		 * @description 获取购物车列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetCarList(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/car/list.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.result==1){
						// this.commit("carList",res.data.data);
						let r=[];
						res.data.data.items.forEach((x)=>{
							x.isSelect=false;
							x.running=1;
							r.push(x);
						})
						res.data.data.items=r;
						resolve(res)
					}else{
						uni.showToast({
							title: '购物车数据获取失败,'+res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description  更新购物车数量
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiUpdateNum(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/car/update.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.result==1){
						this.commit("updateNum",{
							id:_data.id,
							num:_data.num
						})
						resolve(res)
					}else{
						uni.showToast({
							title:res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 添加商品到购物车
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiAddGoodToCar(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/car/add.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title:res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 删除购物车商品
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiDelectGood(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/car/del.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.result==1){
						// this.commit("delGood",_data)
						resolve(res)
					}else{
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 直接提交订单
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiPostOrder(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/mall/order.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @param {Object} context
		 * @param {Object} _data 好课拼购提交订单
		 */
		spellorder(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/spell/order.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 从购物车提交订单
		 * @param {Object} context
		 * @param {Object} _data
		 */
		carPostOrder(context,_data){
			return new Promise((resolve,reject)=>{
				Request().request({
					url:'/car/order.do',
					method:'post',
					data:_data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		}
    }
}

export default car
